/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 17:39:14
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using LEARNREN.COM.DAL.IdentiyHelper;
using System.Linq.Expressions;
using LEARNREN.COM.DAL;
using LEARNREN.COM.BLL.Enums;

namespace LEARNREN.COM.BLL
{

    public abstract class LogicBase<T> : ILogic
    {
        private DB4ODALClient GetDbClient(string conn)
        {
            return DB4ODALServerHelper.GetIDALClient(conn);
        }
        /// <summary>
        /// 主数据库管理类
        /// </summary>
        protected DB4ODALClient mainClient = null;
        public LogicBase(DB4ODALClient client)
        {
            if (client == null)
            {
                string mainConn = System.Configuration.ConfigurationManager.ConnectionStrings["mainBase"].ConnectionString;
                mainClient = GetDbClient(mainConn);
            }
            else
            {
                mainClient = client;
            }
        }
        public LogicBase()
        {
            string mainConn = System.Configuration.ConfigurationManager.ConnectionStrings["mainBase"].ConnectionString;
            mainClient = GetDbClient(mainConn);
        }

        public virtual void Save(T obj)
        {
            mainClient.Save(obj);
        }

        public virtual void Delete(T obj)
        {
            mainClient.Delete(obj);
        }

        /// <summary>
        /// 根据条件筛选
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public virtual List<T> GetList(Predicate<T> p)
        {
            return mainClient.GetList<T>(p);
        }

        /// <summary>
        /// 获取全部对象
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList()
        {
            return mainClient.GetList<T>();
        }

        /// <summary>
        /// 按照一个属性正序排列
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="p"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public List<T> GetListASC<TKey>(Predicate<T> p, Func<T, TKey> keySelector)
        {
            return mainClient.GetListASC(p, keySelector);
        }

        /// <summary>
        /// 按照一个属性倒叙排列
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="p"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public List<T> GetListDESC<TKey>(Predicate<T> p, Expression<Func<T, TKey>> keySelector)
        {
            return mainClient.GetListDESC(p, keySelector);
        }

        /// <summary>
        /// 按照一个属性正序排列
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="p"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public List<T> GetListASC<TKey>(Func<T, TKey> keySelector)
        {
            return mainClient.GetListASC(keySelector);
        }

        /// <summary>
        /// 按照一个属性倒叙排列
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="p"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public List<T> GetListDESC<TKey>(Func<T, TKey> keySelector)
        {
            return mainClient.GetListDESC(keySelector);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="searchCondition">检索条件</param>
        /// <param name="orderKeySelector">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <param name="pageSize">页面大小默认为10</param>
        /// <param name="pageIndex">页面索引</param>
        /// <returns></returns>
        public List<T> GetListByPage<TKey>(Predicate<T> searchCondition = null,
                                            Expression<Func<T, TKey>> orderKeySelector = null,
                                            OrderEnum order = OrderEnum.Ascending,
                                            int pageSize = 10,
                                            int pageIndex = 1)
        {
            DAL.Enum.OrderEnum orderenum;
            if (order == OrderEnum.Ascending)
            {
                orderenum = DAL.Enum.OrderEnum.Ascending;
            }
            else
            {
                orderenum = DAL.Enum.OrderEnum.Descending;
            }
            return mainClient.GetListByPage(searchCondition, orderKeySelector, orderenum, pageSize, pageIndex);
        }

        /// <summary>
        /// 分页查询
        /// </summary> 
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="totalCount">输出总的条目数</param>
        /// <param name="searchCondition">检索条件</param>
        /// <param name="orderKeySelector">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <param name="pageSize">页面大小默认为10</param>
        /// <param name="pageIndex">页面索引</param>
        /// <returns></returns>
        public List<T> GetListByPage<TKey>(out int totalCount,
                                            Predicate<T> searchCondition = null,
                                            Expression<Func<T, TKey>> orderKeySelector = null,
                                            OrderEnum order = OrderEnum.Ascending,
                                            int pageSize = 10,
                                            int pageIndex = 1)
        {
            DAL.Enum.OrderEnum orderenum;
            if (order == OrderEnum.Ascending)
            {
                orderenum = DAL.Enum.OrderEnum.Ascending;
            }
            else
            {
                orderenum = DAL.Enum.OrderEnum.Descending;
            }
            return mainClient.GetListByPage(out totalCount, searchCondition, orderKeySelector, orderenum, pageSize,pageIndex );
        }

        /// <summary>
        /// 根据起始索引和获取数量查询列表
        /// </summary>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="totalCount">输出总的条目数</param>
        /// <param name="searchCondition">检索条件</param>
        /// <param name="orderKeySelector">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <param name="iDisplayStart">起始索引，从0开始。</param>
        /// <param name="iDisplayLength">显示条数</param>
        /// <returns></returns>
        public List<T> GetListByPage2<TKey>(out int totalCount,
                                            Predicate<T> searchCondition = null,
                                            Expression<Func<T, TKey>> orderKeySelector = null,
                                            OrderEnum order = OrderEnum.Ascending,
                                            int iDisplayStart = 0,
                                            int iDisplayLength = 10)
        {
            DAL.Enum.OrderEnum orderenum;
            if (order == OrderEnum.Ascending)
            {
                orderenum = DAL.Enum.OrderEnum.Ascending;
            }
            else
            {
                orderenum = DAL.Enum.OrderEnum.Descending;
            }
            return mainClient.GetListByPage2(out totalCount, searchCondition, orderKeySelector, orderenum, iDisplayStart, iDisplayLength);
        }

        /// <summary>
        /// 获取新的自增Id
        /// </summary>
        /// <returns></returns>
        public long GetNewIdentity()
        {
            Type type = typeof(T);
            return IdentityHelper.GetNewIdentity(mainClient, type);
        }

        public virtual void Dispose()
        {
            if (mainClient != null)
            {
                mainClient.Dispose();
                mainClient = null;
            }
        }
    }
}
